﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7_Team8
{
    class InvertAlgorithm_16bytes : VisionAlgorithm
    {
        public InvertAlgorithm_16bytes(String name) : base(name) { }
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            Bitmap returnImage = new Bitmap(sourceImage);
            Rectangle rectangle = new Rectangle(0, 0, sourceImage.Width, sourceImage.Height);
            BitmapData bitmapData =
                returnImage.LockBits(rectangle, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                returnImage.PixelFormat);

            IntPtr ptr = bitmapData.Scan0;

            int size = Math.Abs(bitmapData.Stride) * sourceImage.Height;
            byte[] rgbValues = new byte[size];

            Marshal.Copy(ptr, rgbValues, 0, size);

            for (int i = 0; i < rgbValues.Length - 16; i = i + 16)
            {
                rgbValues[i] = Convert.ToByte(255 - rgbValues[i]);
                rgbValues[i + 1] = Convert.ToByte(255 - rgbValues[i + 1]);
                rgbValues[i + 2] = Convert.ToByte(255 - rgbValues[i + 2]);

                rgbValues[i + 4] = Convert.ToByte(255 - rgbValues[i + 4]);
                rgbValues[i + 5] = Convert.ToByte(255 - rgbValues[i + 5]);
                rgbValues[i + 6] = Convert.ToByte(255 - rgbValues[i + 6]);

                rgbValues[i + 8] = Convert.ToByte(255 - rgbValues[i + 8]);
                rgbValues[i + 9] = Convert.ToByte(255 - rgbValues[i + 9]);
                rgbValues[i + 10] = Convert.ToByte(255 - rgbValues[i + 10]);

                rgbValues[i + 12] = Convert.ToByte(255 - rgbValues[i + 12]);
                rgbValues[i + 13] = Convert.ToByte(255 - rgbValues[i + 13]);
                rgbValues[i + 14] = Convert.ToByte(255 - rgbValues[i + 14]);
            }


            Marshal.Copy(rgbValues, 0, ptr, size);
            returnImage.UnlockBits(bitmapData);
            return returnImage;
        }
    }
}
